package com.gopay.bis.service.bank.impl.wap;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.common.util.BisPayCheckUtils;
import com.gopay.bis.common.util.constants.BisPayConstants;
import com.gopay.bis.manager.bank.Wap.CcbWapB2cManager;
import com.gopay.bis.manager.bank.Wap.CebWapB2cManager;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.CebBankStatus;
import com.gopay.common.constants.bank.CebContants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.ccb.ebank.pay.CcbB2cMessage;
import com.gopay.common.domain.bank.ceb.ebank.CebB2cMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.WapPayInteractRemoteService;
/**
 * 光大银行B2C业务实现
 * @author wangjiqing
 *
 */
public class CebWapPayB2CBankServiceImpl implements WapPayInteractRemoteService{
	
	@Resource(name="cebWapB2cManager")
	private CebWapB2cManager cebWapB2cManager;
	
	/**
     * 银行支付类请求
     * 
     * @param BaseMessage
     * @return Result
     */
	@Override
	public Result pay(BaseMessage base) {
		Result result=new Result(false);
		try{
			CebB2cMessage messageB2C = new CebB2cMessage();
            BeanUtils.copyProperties(base, messageB2C);
            CebB2cMessage cebB2cMessage =(CebB2cMessage)cebWapB2cManager.sign(messageB2C);
            result.setDefaultModel(cebB2cMessage);
            result.setSuccess(true);			
		}catch(GopayException e){
			BankLogUtils.printServiceImplExceptionLog(e);
            result.setResultCode(e.getErrCode());
		}catch(Exception e){
			BankLogUtils.printServiceImplExceptionLog(e);
            result.setResultCode(ProcCodeConstants.PROC_CODE_100E6029);
		}
		return result;
	}

	/**
     * 银行返回类请求
     * 
     * @param BaseMessage
     * @return Result
     */
	@Override
	public Result bankBack(BaseMessage base) {
	    Result result = new Result(false);
        try {
        	boolean flag = false;
            CebB2cMessage messageB2C = new CebB2cMessage();
            BeanUtils.copyProperties(base, messageB2C);
            flag = (Boolean)cebWapB2cManager.verify(messageB2C);                        
            result.setModel("message", messageB2C);
            result.setDefaultModel(flag);
            result.setSuccess(true);

        	Map<String, String> param = new HashMap<String, String>();
            //光大银行只有成功状态才会回应报文
            if(base.isFront()){
                param.put("orderId", messageB2C.getOrderId());
                param.put("amt", messageB2C.getAmount().toString());
                param.put("cardType", "Unknown");
                param.put("notifyNum", "1");
                param.put("payStatus", "00");
                param.put("retCode", "00");
                param.put("retMsg", "验签成功");
                param.put("signData", messageB2C.getSignMessage());
                param.put("isFront", String.valueOf(base.isFront()));
                result.setModel("isNotifyCashier", param);
                result.setOrderStatus(true);
            }else if (CebContants.CEB_RESPONSE_SUCC.equals(messageB2C.getResponseCode())) {
                param.put("orderId", messageB2C.getOrderId());
                param.put("amt", messageB2C.getAmount().toString());
                param.put("cardType", "Unknown");
                param.put("notifyNum", "1");
                param.put("payStatus", "00");
                param.put("retCode", "00");
                param.put("retMsg", "验签成功");
                param.put("signData", messageB2C.getSignMessage());
                param.put("isFront", String.valueOf(base.isFront()));
                result.setModel("isNotifyCashier", param);
                result.setOrderStatus(true);
            } else {
                // 通知后台地址该订单失败
                param.put("orderId", messageB2C.getOrderId());
                param.put("amt", messageB2C.getAmount().toString());
                param.put("cardType", "Unknown");
                param.put("notifyNum", "1");
                param.put("payStatus", "01");
                param.put("retCode", "01");
                param.put("retMsg", "验签失败");
                param.put("signData", messageB2C.getSignMessage());
                param.put("isFront", String.valueOf(base.isFront()));
                result.setModel("isNotifyCashier", param);
                result.setOrderStatus(false);
            }
        
        } catch (GopayException e) {
            BankLogUtils.printServiceImplExceptionLog(e);
            result.setResultCode(e.getErrCode());
        } catch (Exception e) {
            BankLogUtils.printServiceImplExceptionLog(e);
            result.setResultCode(ProcCodeConstants.PROC_CODE_100E6064);
        }
        return result;
	}

	
	

	/**
     * 银行查询类请求
     * 
     * @param BaseMessage
     * @return Result
     */
	@Override
	public Result query(BaseMessage base) {
		  Result result = new Result(false);
	        try {
	            CebB2cMessage messageB2C = new CebB2cMessage();
	            BeanUtils.copyProperties(base, messageB2C);
	            CebB2cMessage res = (CebB2cMessage) cebWapB2cManager.query(messageB2C);
	            result.setSuccess(true);
	            result.setDefaultModel(res);
	            if(BisPayCheckUtils.checkExternalOrderID(messageB2C.getOrderId())){
	        		Map<String, String> param = new HashMap<String, String>();
	        		param.put("orderId", messageB2C.getOrderId());
	        		param.put("amount", messageB2C.getAmount().toString());
	        		if (res == null) {
	        			// 查询订单为空
	        			param.put("returnCode", BisPayConstants.QUERYSTATE_FAIL);
	        			param.put("returnMessage", "银行返回对账结果为空");
	        			param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
						result.setModel("queryResult", param);
						return result;
	        		}
	        		if (!CebContants.QUERY_RESP_CODE_SUCCESS.equals(res.getResponseCode())) {
	        			// 查询订单错误
	        			param.put("returnCode", BisPayConstants.QUERYSTATE_FAIL);
	        			param.put("returnMessage", "查询订单失败");
	        			param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
						result.setModel("queryResult", param);
						return result;
	    			}			
	        		if (res.getTransStatus() != null && !"".equals(res.getTransStatus())) {
	    				param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);
	    				//成功处理
	    				if (StringUtils.equals(CebContants.CEB_TRANS_STATUS_SUCCESS,res.getTransStatus())) {
	    					param.put("tradeState", BisPayConstants.TRADESTATE_SUCCESS);
	    					param.put("returnMessage", "交易成功");
	    				}
	    				//失败处理
	    				else if(StringUtils.equals(CebContants.CEB_TRANS_STATUS_FAILED,res.getTransStatus())){		
	    					param.put("tradeState", BisPayConstants.TRADESTATE_FAIL);
	    					param.put("returnMessage", "交易失败");
	    				}else {
	    					// 银行订单状态为失败
	    					param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
	    					param.put("returnMessage", CebBankStatus.getName(res.getTransStatus()));
	    				}
	    			} else {
	    				param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);
						param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
						param.put("returnMessage", "银行返回订单未查到");
	    			}
	        		result.setModel("queryResult", param);
	        	}
	        } catch (GopayException e) {
	            BankLogUtils.printServiceImplExceptionLog(e);
	            result.setResultCode(e.getErrCode());
	        } catch (Exception e) {
	            BankLogUtils.printServiceImplExceptionLog(e);
	            result.setResultCode(ProcCodeConstants.PROC_CODE_100E6033);
	        }
	        return result;
	}
	/**
     * 银行退款类请求
     * 
     * @param BaseMessage
     * @return Result
     */
	@Override
	public Result refund(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

	/**
     * 银行日终类请求
     * 
     * @param BaseMessage
     * @return Result
     */
	@Override
	public Result dayend(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public Result queryBack(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

}
